<?php

class Crc_Bootstrap_Resource_View extends Zend_Application_Resource_ResourceAbstract
{
	public function init()
	{
        $logger = $this->getBootstrap()->getResource('log');
        $logger->info('Bootstrap ' . __CLASS__);

		$options = $this->getOptions();
		$view = new Zend_View();

		if (array_key_exists('doctype', $options)) {
			$view->doctype($options['doctype']);
		}

		if (array_key_exists('headTitle', $options)) {
			$view->headTitle()->set($options['headTitle']);
		}

		$view->headTitle()->setSeparator(' - ')
			->setIndent(4);

		if (array_key_exists('httpEquiv', $options)) {
			$view->headMeta()->setIndent(4);
			foreach ($options['httpEquiv'] as $type => $value) {
				$view->headMeta()->appendHttpEquiv($type, $value);
			}
		}

		if (!array_key_exists('minifyVersion', $options)) {
			$options['minifyVersion'] = 0;
		}

		$view->headScript()->setIndent(4);
		if (array_key_exists('scripts', $options)) {
			if (false == $options['minify']) {
				foreach ($options['scripts'] as $script) {
					$view->headScript()->appendFile($script);
				}
			} else {
				$view->headScript()->appendFile(APPLICATION_URL. "/resources/m.php/scripts_{$options['minifyVersion']}.js");
			}
		}

		if (array_key_exists('helperPaths', $options)) {
			foreach ($options['helperPaths'] as $prefix => $path) {
				$view->addHelperPath($path, $prefix);
			}
		}

		if (array_key_exists('scriptPaths', $options)) {
			foreach ($options['scriptPaths'] as $path) {
				$view->addScriptPath($path);
			}
		}

		//$view->addHelperPath('Crc/View/Helper', 'Crc_View_Helper');
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
		$viewRenderer->setView($view);

		return $view;
	}
}